home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1600_niping.c < prev    next >
Text File  |  1995-11-28  |  4KB  |  155 lines

  1. /*
  2.  * niping
  3.  *
  4.  * usage: niping host/tag
  5.  * compile with: cc -o niping niping.c
  6.  *
  7.  * This program connects to a server and gets the ni_id of the 
  8.  * root directory.  Its main function is to test to see if the
  9.  * server is up.  The parse_serve_tag() function is useful for
  10.  * dealing with strings of the form "ranger/network" or
  11.  * "192.42.172.34/local".  It accepts a string as input and
  12.  * gives bask a server's address and tag in the format that
  13.  * are expected by ni_connect().
  14.  *
  15.  *    myhost> niping mustang/local
  16.  *    connected to NetInfo server mustang/local
  17.  *
  18.  *    myhost> niping 192.42.172.34/network
  19.  *    connected to NetInfo server 192.42.172.34/network
  20.  *
  21.  *    myhost> niping mumble
  22.  *    mumble: can't understand arg 
  23.  *    usage: niping host/tag
  24.  *    host can be a host name or IP address
  25.  *
  26.  *    myhost> niping casper/ghost
  27.  *    Communication failure
  28.  *
  29.  * Author: Marc Majka
  30.  * You may freely copy, distribute and reuse the code in this example.  
  31.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  32.  * its fitness for any particular use.
  33.  */
  34.  
  35. #import <netinfo/ni.h>
  36. #import <ansi/stdio.h>
  37. #import <bsd/netinet/in.h>
  38. #import <bsd/sys/socket.h>
  39. #import <bsd/errno.h>
  40. #import <ansi/ctype.h>
  41. #import <bsd/netdb.h>
  42. #import <bsd/c.h>
  43. #import <bsd/strings.h>
  44.  
  45. main(int argc, char *argv[])
  46. {
  47.     char        *tag;
  48.     void        *dom;
  49.     ni_id       rootdir;
  50.     ni_status   ret;
  51.     bool        parse_server_tag();
  52.     struct sockaddr_in  server;
  53.  
  54.     /* check usage */
  55.     if (argc != 2) {
  56.         fprintf(stderr, "usage: %s host/tag\n", argv[0]);
  57.         fprintf(stderr, "host can be a host name or IP address\n");
  58.         exit(1);
  59.     }
  60.  
  61.     /* call a function to parse the input arg */
  62.     if (FALSE == parse_server_tag(argv[1], &server, &tag)) {
  63.         fprintf(stderr, "%s: can't understand arg %s\n", argv[1]);
  64.         fprintf(stderr, "usage: %s host/tag\n", argv[0]);
  65.         fprintf(stderr, "host can be a host name or IP address\n");
  66.         exit(1);
  67.     }
  68.  
  69.     /* connect to the specified server */
  70.     dom = ni_connect(&server, tag);
  71.     if (dom == NULL) {
  72.         fprintf(stderr, "connection to %s failed\n", argv[1]);
  73.         free(tag);
  74.         exit(1);
  75.     }
  76.  
  77.     /* abort on errors */
  78.     ni_setabort(dom, 1);
  79.  
  80.     /* set a 4 second read timeout */
  81.     ni_setreadtimeout(dom, 4);
  82.  
  83.     /* try to get the id of the root directory */
  84.     ret = ni_root(dom, &rootdir);
  85.     if (ret != NI_OK) {
  86.         fprintf(stderr, "%s\n", ni_error(ret));
  87.         exit(1);
  88.     }
  89.  
  90.     printf("connected to NetInfo server %s\n", argv[1]);
  91.  
  92.     /* clean up */
  93.     free(tag);
  94.     ni_free(dom);
  95.  
  96.     exit(0);
  97. }
  98.  
  99. bool parse_server_tag(char *str, struct sockaddr_in *server, char **t)
  100. {
  101.     int len, i;
  102.     char *host, *tag, *slash;
  103.     struct hostent *hent;
  104.  
  105.     len = strlen(str);
  106.  
  107.     /* find the "/" character */
  108.     slash = index(str, '/');
  109.  
  110.     /* check to see if the "/" is missing */
  111.     if (slash == NULL) {
  112.         /* error: no "/" character */
  113.         return(FALSE);
  114.     }
  115.  
  116.     /* find the location of the '/' */
  117.     i = slash - str;
  118.  
  119.     /* allocate some space for the host and tag */
  120.     host = (char *)malloc(i + 1);
  121.     *t = (char *)malloc(len - i);
  122.     tag = *t;
  123.  
  124.     /* copy out the host */
  125.     strncpy(host, str, i);
  126.  
  127.     /* copy out the tag */
  128.     strcpy(tag, slash + 1);
  129.  
  130.     /* assume the host is a hostname */
  131.     hent = gethostbyname(host);
  132.  
  133.     if (hent != NULL) {
  134.         /* found a host with that name */
  135.         bcopy(hent->h_addr, &server->sin_addr, hent->h_length);
  136.     }
  137.     else {
  138.         /* no host with that name */
  139.         /* try interpreting it as an address */
  140.         server->sin_addr.s_addr = inet_addr(host);
  141.  
  142.         /* bail out if inet_addr() returned -1 */
  143.         /* it isn't a sensible address */
  144.         if (server->sin_addr.s_addr == -1) {
  145.             fprintf(stderr, "Can't find address for %s\n", host);
  146.             free(host);
  147.             free(tag);
  148.             return(1);
  149.         }
  150.     }
  151.  
  152.     free(host);
  153.     return(TRUE);
  154. }
  155.